library(here)
library(data.table)
library(PanelMatch)
library(ggplot2)

source(here('scripts', 'plot_function.R'))

fullData <- fread(here('data', 'full_rol.csv'))

# Create Subsets --------------------------------------------------------------------

fullData$low_rol <- 1

for (r in which(fullData$v2x_rule > .8 & fullData$ml_trans == 1)) {
  country <- fullData[r, iso3c]
  fullData[iso3c == country & ml == 1, low_rol := 0]
}

fullData$high_rol <- 1

for (r in which(fullData$v2x_rule <= .8 & fullData$ml_trans == 1)) {
  country <- fullData[r, iso3c]
  fullData[iso3c == country & ml == 1, high_rol := 0]
}

rm(country, r)

# Figure 3 --------------------------------------------------------------------------

## Full -----------------------------------------------------------------------------

pm_full <- PanelMatch(lag = 5, lead = 0:5,
                         time.id = "year", unit.id = "cid_pm", 
                         treatment = "ml", 
                         outcome.var = "v2x_rule",
                         refinement.method = "CBPS.weight", 
                         covs.formula = ~ lag(v2x_rule, 1) +
                           lag(bits_ln, 1) +
                           lag(market_ln, 1) +
                           lag(gdppc_ln, 1) +
                           lag(trade_ln, 1) +
                           lag(fdi_stock_ln, 1) +
                           lag(growth, 1) +
                           lag(nyc, 1),
                         qoi = "att", 
                         use.diagonal.variance.matrix = TRUE,
                         forbid.treatment.reversal = TRUE,
                         data = as.data.frame(fullData))

# Get data for plotting
plotData_full <- get_plot_data(pm_full, 
                               f = -5:5,
                               data = as.data.frame(fullData),
                               iterations = 5000)

## Low - CPBS ---------------------------------------------------------------------------

pm_low <- PanelMatch(lag = 5, lead = 0:5,
                     time.id = "year", unit.id = "cid_pm", 
                     treatment = "ml", 
                     outcome.var = "v2x_rule",
                     refinement.method = "CBPS.weight", 
                     covs.formula = ~ lag(v2x_rule, 1) +
                       lag(bits_ln, 1) +
                       lag(market_ln, 1) +
                       lag(gdppc_ln, 1) +
                       lag(trade_ln, 1) +
                       lag(fdi_stock_ln, 1) +
                       lag(growth, 1) +
                       lag(nyc, 1),
                     qoi = "att", 
                     use.diagonal.variance.matrix = TRUE,
                     forbid.treatment.reversal = TRUE,
                     data = as.data.frame(fullData[fullData$low_rol == 1, ]))

# Get data for plotting
plotData_low <- get_plot_data(pm_low, 
                              f = -5:5,
                              data = as.data.frame(fullData[fullData$low_rol == 1, ]),
                              iterations = 5000,
                              seed = 10)


## Low - PS ----------------------------------------------------------------------

set.seed(10)

pm_low_ps <- PanelMatch(lag = 5, lead = 0:5,
                     time.id = "year", unit.id = "cid_pm", 
                     treatment = "ml", 
                     outcome.var = "v2x_rule",
                     refinement.method = "ps.weight", 
                     covs.formula = ~ lag(v2x_rule, 1) +
                       lag(bits_ln, 1) +
                       lag(market_ln, 1) +
                       lag(gdppc_ln, 1) +
                       lag(trade_ln, 1) +
                       lag(fdi_stock_ln, 1) +
                       lag(growth, 1) +
                       lag(nyc, 1),
                     qoi = "att", 
                     use.diagonal.variance.matrix = TRUE,
                     forbid.treatment.reversal = TRUE,
                     data = as.data.frame(fullData[fullData$low_rol == 1, ]))

pe_low_ps <- PanelEstimate(pm_low_ps, number.iterations = 5000, 
                           data = as.data.frame(fullData[fullData$low_rol == 1, ]))
summary(pe_low_ps)

## High -----------------------------------------------------------------------------

pm_high <- PanelMatch(lag = 5, lead = 0:5,
                     time.id = "year", unit.id = "cid_pm", 
                     treatment = "ml", 
                     outcome.var = "v2x_rule",
                     refinement.method = "CBPS.weight", 
                     covs.formula = ~ lag(v2x_rule, 1) +
                       lag(bits_ln, 1) +
                       lag(market_ln, 1) +
                       lag(gdppc_ln, 1) +
                       lag(trade_ln, 1) +
                       lag(fdi_stock_ln, 1) +
                       lag(growth, 1) +
                       lag(nyc, 1),
                     qoi = "att", 
                     use.diagonal.variance.matrix = TRUE,
                     forbid.treatment.reversal = TRUE,
                     data = as.data.frame(fullData[fullData$high_rol == 1, ]))

plotData_high <- get_plot_data(pm_high, 
                              f = -5:5,
                              data = as.data.frame(fullData[fullData$high_rol == 1, ]),
                              iterations = 5000,
                              seed = 10)

## Create plots ---------------------------------------------------------------------

ggplot(plotData_full,
       aes(x = t, y = estimate)) +
  geom_hline(yintercept = 0, color = 'red', size = .5, linetype = 'dashed') +
  geom_vline(xintercept = -.5, color = 'black', size = .5, linetype = 'dashed', alpha = .5) +
  geom_point(size = 4.5) +
  geom_line() +
  geom_errorbar(aes(ymin = ci.025, ymax = ci.975), 
                width=.2, size = .5) +
  geom_errorbar(aes(ymin = ci.5, ymax = ci.95), 
                width=0, size = 1.75) +
  scale_x_continuous(breaks = -5:5,
                     labels = -5:5) +
  scale_y_continuous(limits = c(-.1,.05),
                     breaks = round(seq(-.175, .05, .025), 3)) +
  theme_bw(base_size = 18) +
  labs(y = 'Est. Change in Rule of Law',
       x = 'Years from Model Law Enactment') +
  theme(panel.grid = element_blank())

ggsave(here('output', 'figures', 'figure-3a.pdf'),
       height = 4, width = 5.5)

ggplot(plotData_low,
       aes(x = t, y = estimate)) +
  geom_hline(yintercept = 0, color = 'red', size = .5, linetype = 'dashed') +
  geom_vline(xintercept = -.5, color = 'black', size = .5, linetype = 'dashed', alpha = .5) +
  geom_point(size = 4.5) +
  geom_line() +
  geom_errorbar(aes(ymin = ci.025, ymax = ci.975), 
                width=.2, size = .5) +
  geom_errorbar(aes(ymin = ci.5, ymax = ci.95), 
                width=0, size = 1.75) +
  scale_x_continuous(breaks = -5:5,
                     labels = -5:5) +
  scale_y_continuous(limits = c(-.1,.05),
                     breaks = round(seq(-.175, .05, .025), 3)) +
  theme_bw(base_size = 18) +
  labs(y = element_blank(),
       x = element_blank()) +
  theme(panel.grid = element_blank())

ggsave(here('output', 'figures', 'figure-3b.pdf'),
       height = 4, width = 5.5)

ggplot(plotData_high,
       aes(x = t, y = estimate)) +
  geom_hline(yintercept = 0, color = 'red', size = .5, linetype = 'dashed') +
  geom_vline(xintercept = -.5, color = 'black', size = .5, linetype = 'dashed', alpha = .5) +
  geom_point(size = 4.5) +
  geom_line() +
  geom_errorbar(aes(ymin = ci.025, ymax = ci.975), 
                width=.2, size = .5) +
  geom_errorbar(aes(ymin = ci.5, ymax = ci.95), 
                width=0, size = 1.75) +
  scale_x_continuous(breaks = -5:5,
                     labels = -5:5) +
  scale_y_continuous(limits = c(-.1,.05),
                     breaks = round(seq(-.175, .05, .025), 3)) +
  theme_bw(base_size = 18) +
  labs(y = element_blank(),
       x = element_blank()) +
  theme(panel.grid = element_blank())

ggsave(here('output', 'figures', 'figure-3c.pdf'),
       height = 4, width = 5.5)

# Table A2 - Low Rule of Law Sample ---------------------------------------------------

# Select countries included in Low Rule of Law analysis 
countries_low <- names(pm_low$att)
countries_low <- strsplit(countries_low, '.', fixed = TRUE)
countries_low <- as.integer(sapply(countries_low, '[[', 1))

temp <- fullData[cid_pm %in% countries_low & ml_trans == 1 , 
                 .(country_name, year, round(v2x_rule, 2))]

temp[order(temp$country_name)]

# Table A3 - High Rule of Law Sample -------------------------------------------------

# Select countries included in High Rule of Law analysis 
countries_high <- names(pm_high$att)
countries_high <- strsplit(countries_high, '.', fixed = TRUE)
countries_high <- as.integer(sapply(countries_high, '[[', 1))

temp <- fullData[cid_pm %in% countries_high & ml_trans == 1 , 
                 .(country_name, year, round(v2x_rule, 2))]

temp[order(temp$country_name)]

# Table A5 - Full PanelMatch Results -------------------------------------------------

# 1 - Full 
round(plotData_full[plotData_full$t >= 0, c('estimate', 'se')], 3)
# Number of ML countries
length(pm_full$att)

# 2 - Low RoL CBPS
round(plotData_low[plotData_low$t >= 0, c('estimate', 'se')], 3)
# Number of ML countries
length(pm_low$att)

# 3 - Low RoL PS
round(summary(pe_low_ps)$summary, 3)
length(pm_low_ps$att)

# 4 - High RoL
round(plotData_high[plotData_high$t >= 0, c('estimate', 'se')], 3)
# Number of ML countries
length(pm_high$att)

# Column 5 - BJS Estiamtes in Stata ("scripts/table-A5-BJS.do")

# Figure A1 - Covariate Balance -----------------------------------------------------

pdf(here('output', 'figures', 'figure-A1.pdf'),
    width = 5, height = 5)
balance_scatter(pm_low,
                covariates = c('v2x_rule',
                               'bits_ln',
                               'market_ln',
                               'gdppc_ln',
                               'trade_ln',
                               'fdi_stock_ln',
                               'growth',
                               'nyc'),
                data = as.data.frame(fullData[low_rol == 1, ]),
                ylim = c(0,.9), xlim = c(0, .9))

dev.off()

# Figure A2 - 10-year window --------------------------------------------------------

pm_low <- PanelMatch(lag = 5, lead = 0:10,
                     time.id = "year", unit.id = "cid_pm", 
                     treatment = "ml", 
                     outcome.var = "v2x_rule",
                     refinement.method = "CBPS.weight", 
                     covs.formula = ~ lag(v2x_rule, 1) +
                       lag(bits_ln, 1) +
                       lag(market_ln, 1) +
                       lag(gdppc_ln, 1) +
                       lag(trade_ln, 1) +
                       lag(fdi_stock_ln, 1) +
                       lag(growth, 1) +
                       lag(nyc, 1),
                     qoi = "att", 
                     use.diagonal.variance.matrix = TRUE,
                     forbid.treatment.reversal = TRUE,
                     data = as.data.frame(fullData[fullData$low_rol == 1, ]))

plotData_low_10 <- get_plot_data(pm_low, 
                                 f = -5:10,
                                 data = as.data.frame(fullData[fullData$low_rol == 1, ]),
                                 iterations = 5000,
                                 seed = 10)

## Create Plot ------------------------------------------------------------------------

ggplot(plotData_low_10,
       aes(x = t, y = estimate)) +
  geom_hline(yintercept = 0, color = 'red', size = .5, linetype = 'dashed') +
  geom_vline(xintercept = -.5, color = 'black', size = .5, linetype = 'dashed', alpha = .5) +
  geom_point(size = 4.5) +
  geom_line() +
  geom_errorbar(aes(ymin = ci.025, ymax = ci.975), 
                width=.2, size = .5) +
  geom_errorbar(aes(ymin = ci.5, ymax = ci.95), 
                width=0, size = 1.75) +
  scale_x_continuous(breaks = -5:10,
                     labels = -5:10) +
  scale_y_continuous(limits = c(-.16,.031),
                     breaks = round(seq(-.175, .05, .025), 3)) +
  theme_bw(base_size = 18) +
  labs(y = 'Est. Change in Rule of Law') +
  labs(y = element_blank(),
       x = element_blank()) +
  theme(panel.grid = element_blank())

ggsave(here('output', 'figures', 'figure-A2.pdf'),
       height = 4, width = 7)

# Figure A3 - No Arbitration Hubs ---------------------------------------------------

hubs <- c('USA', 'GBR', 'FRA', 'CHE', 'SWE')

fullData[ , ml_nohubs := ml]
fullData[ iso3c %in% hubs , ml_nohubs := 1]


## Figure A3a - Full Sample ---------------------------------------------------------

pm_nohubs_full <- PanelMatch(lag = 5, lead = 0:5,
                             time.id = "year", 
                             unit.id = "cid_pm", 
                             treatment = "ml_nohubs", 
                             outcome.var = "v2x_rule",
                             refinement.method = "CBPS.weight", 
                             covs.formula = ~ lag(v2x_rule, 1) +
                               lag(bits_ln, 1) +
                               lag(market_ln, 1) +
                               lag(gdppc_ln, 1) +
                               lag(trade_ln, 1) +
                               lag(fdi_stock_ln, 1) +
                               lag(growth, 1) +
                               lag(nyc, 1),
                             qoi = "att", 
                             use.diagonal.variance.matrix = TRUE,
                             forbid.treatment.reversal = TRUE,
                             data = as.data.frame(fullData))

plotData_nohubs_full <- get_plot_data(pm_nohubs_full, 
                                      f = -5:5,
                                      data = as.data.frame(fullData),
                                      iterations = 5000,
                                      seed = 10)


## Figure A3b - Low Rule of Law Sample ----------------------------------------------

pm_nohubs_low <- PanelMatch(lag = 5, lead = 0:5,
                             time.id = "year", 
                             unit.id = "cid_pm", 
                             treatment = "ml_nohubs", 
                             outcome.var = "v2x_rule",
                             refinement.method = "CBPS.weight", 
                             covs.formula = ~ lag(v2x_rule, 1) +
                               lag(bits_ln, 1) +
                               lag(market_ln, 1) +
                               lag(gdppc_ln, 1) +
                               lag(trade_ln, 1) +
                               lag(fdi_stock_ln, 1) +
                               lag(growth, 1) +
                               lag(nyc, 1),
                             qoi = "att", 
                             use.diagonal.variance.matrix = TRUE,
                             forbid.treatment.reversal = TRUE,
                             data = as.data.frame(fullData[low_rol == 1, ]))

plotData_nohubs_low <- get_plot_data(pm_nohubs_low, 
                                      f = -5:5,
                                      data = as.data.frame(fullData[low_rol == 1, ]),
                                      iterations = 5000,
                                      seed = 10)

## Create plots ---------------------------------------------------------------------

ggplot(plotData_nohubs_full,
       aes(x = t, y = estimate)) +
  geom_hline(yintercept = 0, color = 'red', size = .5, linetype = 'dashed') +
  geom_vline(xintercept = -.5, color = 'black', size = .5, linetype = 'dashed', alpha = .5) +
  geom_point(size = 4.5) +
  geom_line() +
  geom_errorbar(aes(ymin = ci.025, ymax = ci.975), 
                width=.2, size = .5) +
  geom_errorbar(aes(ymin = ci.5, ymax = ci.95), 
                width=0, size = 1.75) +
  scale_x_continuous(breaks = -5:5,
                     labels = -5:5) +
  scale_y_continuous(limits = c(-.105,.05),
                     breaks = round(seq(-.175, .05, .025), 3)) +
  theme_bw(base_size = 18) +
  labs(y = 'Est. Change in Rule of Law',
       x = 'Years from Model Law Enactment') +
  theme(panel.grid = element_blank())

ggsave(here('output', 'figures', 'figure-A3-a.pdf'),
       height = 4, width = 5.5)

ggplot(plotData_nohubs_low,
       aes(x = t, y = estimate)) +
  geom_hline(yintercept = 0, color = 'red', size = .5, linetype = 'dashed') +
  geom_vline(xintercept = -.5, color = 'black', size = .5, linetype = 'dashed', alpha = .5) +
  geom_point(size = 4.5) +
  geom_line() +
  geom_errorbar(aes(ymin = ci.025, ymax = ci.975), 
                width=.2, size = .5) +
  geom_errorbar(aes(ymin = ci.5, ymax = ci.95), 
                width=0, size = 1.75) +
  scale_x_continuous(breaks = -5:5,
                     labels = -5:5) +
  scale_y_continuous(limits = c(-.105,.05),
                     breaks = round(seq(-.175, .05, .025), 3)) +
  theme_bw(base_size = 18) +
  labs(y = 'Est. Change in Rule of Law',
       x = 'Years from Model Law Enactment') +
  theme(panel.grid = element_blank())

ggsave(here('output', 'figures', 'figure-A3-b.pdf'),
       height = 4, width = 5.5)


# Figure A5 - Adj. for Polyarchy ----------------------------------------------------

## Figure A5a - Full Sample ---------------------------------------------------------

pm_poly_full <- PanelMatch(lag = 5, lead = 0:5,
                           time.id = "year", unit.id = "cid_pm", 
                           treatment = "ml", 
                           outcome.var = "v2x_rule",
                           refinement.method = "CBPS.weight", 
                           covs.formula = ~ lag(v2x_rule, 1) +
                             lag(v2x_polyarchy, 1) +
                             lag(bits_ln, 1) +
                             lag(market_ln, 1) +
                             lag(gdppc_ln, 1) +
                             lag(trade_ln, 1) +
                             lag(fdi_stock_ln, 1) +
                             lag(growth, 1) +
                             lag(nyc, 1),
                           qoi = "att", 
                           use.diagonal.variance.matrix = TRUE,
                           forbid.treatment.reversal = TRUE,
                           data = as.data.frame(fullData))

plotData_poly_full <- get_plot_data(pm_poly_full, 
                                    f = -5:5,
                                    data = as.data.frame(fullData),
                                    iterations = 5000,
                                    seed = 10)

## Figure A5b - Low Rule of Law -----------------------------------------------------

pm_poly_low <- PanelMatch(lag = 5, lead = 0:5,
                     time.id = "year", unit.id = "cid_pm", 
                     treatment = "ml", 
                     outcome.var = "v2x_rule",
                     refinement.method = "CBPS.weight", 
                     covs.formula = ~ lag(v2x_rule, 1) +
                       lag(v2x_polyarchy, 1) +
                       lag(bits_ln, 1) +
                       lag(market_ln, 1) +
                       lag(gdppc_ln, 1) +
                       lag(trade_ln, 1) +
                       lag(fdi_stock_ln, 1) +
                       lag(growth, 1) +
                       lag(nyc, 1),
                     qoi = "att", 
                     use.diagonal.variance.matrix = TRUE,
                     forbid.treatment.reversal = TRUE,
                     data = as.data.frame(fullData[fullData$low_rol == 1, ]))

plotData_poly_low <- get_plot_data(pm_poly_low, 
                                   f = -5:5,
                                   data = as.data.frame(fullData[fullData$low_rol == 1, ]),
                                   iterations = 5000,
                                   seed = 10)

## Create plots ---------------------------------------------------------------------

ggplot(plotData_poly_full,
       aes(x = t, y = estimate)) +
  geom_hline(yintercept = 0, color = 'red', size = .5, linetype = 'dashed') +
  geom_vline(xintercept = -.5, color = 'black', size = .5, linetype = 'dashed', alpha = .5) +
  geom_point(size = 4.5) +
  geom_line() +
  geom_errorbar(aes(ymin = ci.025, ymax = ci.975), 
                width=.2, size = .5) +
  geom_errorbar(aes(ymin = ci.5, ymax = ci.95), 
                width=0, size = 1.75) +
  scale_x_continuous(breaks = -5:5,
                     labels = -5:5) +
  scale_y_continuous(limits = c(-.105,.05),
                     breaks = round(seq(-.175, .05, .025), 3)) +
  theme_bw(base_size = 18) +
  labs(y = 'Est. Change in Rule of Law',
       x = 'Years from Model Law Enactment') +
  theme(panel.grid = element_blank())

ggsave(here('output', 'figures', 'figure-A5-a.pdf'),
       height = 4, width = 5.5)

ggplot(plotData_poly_low,
       aes(x = t, y = estimate)) +
  geom_hline(yintercept = 0, color = 'red', size = .5, linetype = 'dashed') +
  geom_vline(xintercept = -.5, color = 'black', size = .5, linetype = 'dashed', alpha = .5) +
  geom_point(size = 4.5) +
  geom_line() +
  geom_errorbar(aes(ymin = ci.025, ymax = ci.975), 
                width=.2, size = .5) +
  geom_errorbar(aes(ymin = ci.5, ymax = ci.95), 
                width=0, size = 1.75) +
  scale_x_continuous(breaks = -5:5,
                     labels = -5:5) +
  scale_y_continuous(limits = c(-.105,.05),
                     breaks = round(seq(-.175, .05, .025), 3)) +
  theme_bw(base_size = 18) +
  labs(y = 'Est. Change in Rule of Law',
       x = 'Years from Model Law Enactment') +
  theme(panel.grid = element_blank())

ggsave(here('output', 'figures', 'figure-A5-b.pdf'),
       height = 4, width = 5.5)

# Figure A6 - Analyses --------------------------------------------------------------

## A6a - Judicial Independence -------------------------------------------------------------

cutoff <- summary(fullData$judicial_independence)['3rd Qu.']

fullData$low_rol <- 1

for (r in which(fullData$judicial_independence > cutoff & fullData$ml_trans == 1)) {
  country <- fullData[r, iso3c]
  fullData[iso3c == country & ml == 1, low_rol := 0]
}

pm_judindep <- PanelMatch(lag = 5, lead = 0:10,
                          time.id = "year", 
                          unit.id = "cid_pm", 
                          treatment = "ml", 
                          outcome.var = "judicial_independence",
                          refinement.method = "CBPS.weight", 
                          covs.formula = ~ lag(judicial_independence, 1) +
                            lag(bits_ln, 1) +
                            lag(market_ln, 1) +
                            lag(gdppc_ln, 1) +
                            lag(trade_ln, 1) +
                            lag(fdi_stock_ln, 1) +
                            lag(growth, 1) +
                            lag(nyc, 1),
                          qoi = "att", 
                          use.diagonal.variance.matrix = TRUE,
                          forbid.treatment.reversal = TRUE,
                          data = as.data.frame(fullData[fullData$low_rol == 1, ]))

plotData_judindep <- get_plot_data(pm_judindep, 
                                   f = -5:10,
                                   data = as.data.frame(fullData[fullData$low_rol == 1, ]),
                                   iterations = 5000,
                                   seed = 10)

## A6b - Integrity of Legal System -------------------------------------------------------------

cutoff <- summary(fullData$integrity_legal_system)['3rd Qu.']

fullData$low_rol <- 1

for (r in which(fullData$integrity_legal_system > cutoff & fullData$ml_trans == 1)) {
  country <- fullData[r, iso3c]
  fullData[iso3c == country & ml == 1, low_rol := 0]
}

pm_legalinteg <- PanelMatch(lag = 5, lead = 0:10,
                            time.id = "year", 
                            unit.id = "cid_pm", 
                            treatment = "ml", 
                            outcome.var = "integrity_legal_system",
                            refinement.method = "CBPS.weight", 
                            covs.formula = ~ lag(integrity_legal_system, 1) +
                              lag(bits_ln, 1) +
                              lag(market_ln, 1) +
                              lag(gdppc_ln, 1) +
                              lag(trade_ln, 1) +
                              lag(fdi_stock_ln, 1) +
                              lag(growth, 1) +
                              lag(nyc, 1),
                            qoi = "att", 
                            use.diagonal.variance.matrix = TRUE,
                            forbid.treatment.reversal = TRUE,
                            data = as.data.frame(fullData[fullData$low_rol == 1, ]))

plotData_legalinteg <- get_plot_data(pm_legalinteg, 
                                     f = -5:10,
                                     data = as.data.frame(fullData[fullData$low_rol == 1, ]),
                                     iterations = 5000,
                                     seed = 10)

## A6c - Contract Enforcement -------------------------------------------------------------

cutoff <- summary(fullData$contract_enforcement)['3rd Qu.']

fullData$low_rol <- 1

for (r in which(fullData$contract_enforcement > cutoff & fullData$ml_trans == 1)) {
  country <- fullData[r, iso3c]
  fullData[iso3c == country & ml == 1, low_rol := 0]
}

pm_contenf <- PanelMatch(lag = 5, lead = 0:10,
                         time.id = "year", 
                         unit.id = "cid_pm", 
                         treatment = "ml", 
                         outcome.var = "contract_enforcement",
                         refinement.method = "CBPS.weight", 
                         covs.formula = ~ lag(contract_enforcement, 1) +
                           lag(bits_ln, 1) +
                           lag(market_ln, 1) +
                           lag(gdppc_ln, 1) +
                           lag(trade_ln, 1) +
                           lag(fdi_stock_ln, 1) +
                           lag(growth, 1) +
                           lag(nyc, 1),
                         qoi = "att", 
                         use.diagonal.variance.matrix = TRUE,
                         forbid.treatment.reversal = TRUE,
                         data = as.data.frame(fullData[fullData$low_rol == 1, ]))

plotData_contenf <- get_plot_data(pm_contenf, 
                                  f = -5:10,
                                  data = as.data.frame(fullData[fullData$low_rol == 1, ]),
                                  iterations = 5000,
                                  seed = 10)

## A6d - Courts Impartial -------------------------------------------------------------

cutoff <- summary(fullData$impartial_courts)['3rd Qu.']

fullData$low_rol <- 1

for (r in which(fullData$impartial_courts > cutoff & fullData$ml_trans == 1)) {
  country <- fullData[r, iso3c]
  fullData[iso3c == country & ml == 1, low_rol := 0]
}

pm_impartial_courts <- PanelMatch(lag = 5, lead = 0:10,
                                  time.id = "year", 
                                  unit.id = "cid_pm", 
                                  treatment = "ml", 
                                  outcome.var = "impartial_courts",
                                  refinement.method = "CBPS.weight", 
                                  covs.formula = ~ lag(impartial_courts, 1) +
                                    lag(bits_ln, 1) +
                                    lag(market_ln, 1) +
                                    lag(gdppc_ln, 1) +
                                    lag(trade_ln, 1) +
                                    lag(fdi_stock_ln, 1) +
                                    lag(growth, 1) +
                                    lag(nyc, 1),
                                  qoi = "att", 
                                  use.diagonal.variance.matrix = TRUE,
                                  forbid.treatment.reversal = TRUE,
                                  data = as.data.frame(fullData[fullData$low_rol == 1, ]))

plotData_impartial_courts <- get_plot_data(pm_impartial_courts, 
                                           f = -5:10,
                                           data = as.data.frame(fullData[fullData$low_rol == 1, ]),
                                           iterations = 5000,
                                           seed = 10)

## Create Plots ---------------------------------------------------------------------

# Judicial Independence
ggplot(plotData_judindep,
       aes(x = t, y = estimate)) +
  geom_hline(yintercept = 0, color = 'red', size = .5, linetype = 'dashed') +
  geom_vline(xintercept = -.5, color = 'black', size = .5, linetype = 'dashed', alpha = .5) +
  geom_line() +
  geom_errorbar(aes(ymin = ci.025, ymax = ci.975), 
                width=.3, size = .5) +
  geom_errorbar(aes(ymin = ci.5, ymax = ci.95), 
                width=0, size = 1.25) +
  scale_x_continuous(breaks = -5:10,
                     labels = -5:10) +
  geom_point(size = 2.5, color = 'black', fill = 'white', 
             shape = 21) +
  #scale_y_continuous(limits = c(-.1,.05),
  #                   breaks = round(seq(-.175, .05, .025), 3)) +
  theme_bw(base_size = 16) +
  labs(y = 'Est. Change in Rule of Law',
       x = 'Years from Model Law Enactment') +
  theme(panel.grid = element_blank())

ggsave(here('output', 'figures', 'figure-A6-a.pdf'),
       height = 4, width = 5)

# Integrity of the Legal System

ggplot(plotData_legalinteg,
       aes(x = t, y = estimate)) +
  geom_hline(yintercept = 0, color = 'red', size = .5, linetype = 'dashed') +
  geom_vline(xintercept = -.5, color = 'black', size = .5, linetype = 'dashed', alpha = .5) +
  geom_line() +
  geom_errorbar(aes(ymin = ci.025, ymax = ci.975), 
                width=.3, size = .5) +
  geom_errorbar(aes(ymin = ci.5, ymax = ci.95), 
                width=0, size = 1.25) +
  scale_x_continuous(breaks = -5:10,
                     labels = -5:10) +
  geom_point(size = 2.5, color = 'black', fill = 'white', 
             shape = 21) +
  #scale_y_continuous(limits = c(-.1,.05),
  #                   breaks = round(seq(-.175, .05, .025), 3)) +
  theme_bw(base_size = 16) +
  labs(y = 'Est. Change in Rule of Law',
       x = 'Years from Model Law Enactment') +
  theme(panel.grid = element_blank())

ggsave(here('output', 'figures', 'figure-A6-b.pdf'),
       height = 4, width = 5)

# Contract Enforcement

ggplot(plotData_contenf,
       aes(x = t, y = estimate)) +
  geom_hline(yintercept = 0, color = 'red', size = .5, linetype = 'dashed') +
  geom_vline(xintercept = -.5, color = 'black', size = .5, linetype = 'dashed', alpha = .5) +
  geom_line() +
  geom_errorbar(aes(ymin = ci.025, ymax = ci.975), 
                width=.3, size = .5) +
  geom_errorbar(aes(ymin = ci.5, ymax = ci.95), 
                width=0, size = 1.25) +
  scale_x_continuous(breaks = -5:10,
                     labels = -5:10) +
  geom_point(size = 2.5, color = 'black', fill = 'white', 
             shape = 21) +
  #scale_y_continuous(limits = c(-.1,.05),
  #                   breaks = round(seq(-.175, .05, .025), 3)) +
  theme_bw(base_size = 16) +
  labs(y = 'Est. Change in Rule of Law',
       x = 'Years from Model Law Enactment') +
  theme(panel.grid = element_blank())

ggsave(here('output', 'figures', 'figure-A6-c.pdf'),
       height = 4, width = 5)

# Impartial Courts
ggplot(plotData_impartial_courts,
       aes(x = t, y = estimate)) +
  geom_hline(yintercept = 0, color = 'red', size = .5, linetype = 'dashed') +
  geom_vline(xintercept = -.5, color = 'black', size = .5, linetype = 'dashed', alpha = .5) +
  geom_line() +
  geom_errorbar(aes(ymin = ci.025, ymax = ci.975), 
                width=.3, size = .5) +
  geom_errorbar(aes(ymin = ci.5, ymax = ci.95), 
                width=0, size = 1.25) +
  scale_x_continuous(breaks = -5:10,
                     labels = -5:10) +
  geom_point(size = 2.5, color = 'black', fill = 'white', 
             shape = 21) +
  #scale_y_continuous(limits = c(-.1,.05),
  #                   breaks = round(seq(-.175, .05, .025), 3)) +
  theme_bw(base_size = 16) +
  labs(y = 'Est. Change in Rule of Law',
       x = 'Years from Model Law Enactment') +
  theme(panel.grid = element_blank())

ggsave(here('output', 'figures', 'figure-A6-d.pdf'),
       height = 4, width = 5)


